/**
 *
 */
package com.wedroid.api.handler.impl;

import java.util.Optional;

import org.apache.commons.lang3.ArrayUtils;
import org.hashids.Hashids;
import org.springframework.stereotype.Component;

import com.wedroid.api.handler.HashidsDecoder;
import com.wedroid.entity.Constant;

/**
 * @author lute
 */
@Component("hashidsDecoder")
public class DefaultHashidsDecoder implements HashidsDecoder {

    private static final Hashids hashids = new Hashids(Constant.HASHIDS_SALT, Constant.HASHIDS_MIN_LENGTH);

    /* (non-Javadoc)
     * @see com.wedroid.api.handler.HashidsDecoder#decode(java.lang.String)
     */
    public Optional<Long> decode(String hashid) {
        long[] ids = hashids.decode(hashid);
        return ArrayUtils.isNotEmpty(ids) ? Optional.of(Long.valueOf(ids[0])) : Optional.empty();
    }

}
